/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.theme.impl;

import java.util.List;

import org.grayrabbit.cms.dao.theme.ThemeDao;
import org.grayrabbit.cms.entity.Theme;
import org.grayrabbit.cms.vo.theme.ThemeBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Theme</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:57:47 
 */
@Repository
public class ThemeDaoImpl extends GenericHibernateDao<Theme, String>
		implements ThemeDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Theme as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see ThemeDao#assembly(ThemeBean)
	 */
	public QueryCondition assembly(ThemeBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see ThemeDao#assemblyCount(ThemeBean)
	 */
	public QueryCondition assemblyCount(ThemeBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see ThemeDao#count(ThemeBean)
	 */
	public int count(ThemeBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see ThemeDao#assemblyNameQuery(ThemeBean)
	 */
	public QueryCondition assermblyNameQuery(ThemeBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see ThemeDao#assemblyNameQueryCount(ThemeBean)
	 */
	public QueryCondition assemblyNameQueryCount(ThemeBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see ThemeDao#findByPage(Page, ThemeBean)
	 */
	public List<Theme> findByPage(Page<Theme> page,
			ThemeBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(ThemeBean bean,QueryCondition qc){
 
	}

	public boolean checkUsableTheme(String themeName) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" and obj.themeName = ? ", themeName);
		qc.add(" and obj.themeStatus = ? ", new Boolean(true));
		int num = findByQc(qc).size();
		if(num >0){
			return true;
		}
		return false;
	}


}